package chap05;

import javax.naming.Name;
import java.sql.Array;
import java.util.*;

public class lterator_demo {
    public static void main(String[] args) {
        System.out.println("Collection:");
        Collection();
        System.out.println("实现iterator");
        Iterator_demo();
        System.out.println("实现arrayList");
        LinkedList();
        System.out.println("实现Hashset");
        Hashset();
        System.out.println("实现Hashset操作一下类");
        HashSet_2();
        System.out.println("实现TreeSet");
        TreeSet();
        System.out.println("使用treeset 重写他的类实例_来实现自然排序");
        TreeSet_Comparable();
        System.out.println("实现HashMap");
        Map_demo();
        System.out.println("HashMap 实现");
        Map_demo_2();
        System.out.println("TreeMap 实现");
        TreeMap();
        test();
    }

    public static void Collection() {
        Collection collection1 = new ArrayList();//创建一个
        collection1.add("000");
        collection1.add("111");
        collection1.add("222");
        System.out.println("集合collection1的大小" + collection1.size());
        System.out.println("输出collection的内容" + collection1);
        collection1.remove("000");//这个岂不是很傻逼
        System.out.println("输出collection的内容" + collection1);
        System.out.println("是否该包含 某个数据" + collection1.contains("111"));
        Collection collection2 = new ArrayList();
        collection2.addAll(collection1);//更像把东西复制过来 过来（over）
        System.out.println("集合collection2的内容" + collection2);
        collection2.clear();
        System.out.println("集合collection2的内容" + collection2);
        Object s[] = collection1.toArray();
        for (int i = 0; i < s.length; i++) {
            System.out.println(s[i]);
        }
    }

    public static void Iterator_demo() {
        Collection collection = new ArrayList();
        collection.add("s1");
        collection.add("s2");
        collection.add("s3");
        Iterator iterator = collection.iterator();
        while (iterator.hasNext())//这是一直到尽头吗    有一种总指针的感觉
        {
            Object element = iterator.next();
            System.out.println("Iterator=" + element);
        }
        if (collection.isEmpty()) System.out.println("it is empty");
        else System.out.println("collection is not Empty size=" + collection.size());
        Iterator iterator2 = collection.iterator();
        while (iterator2.hasNext()) {
            Object element = iterator2.next();
            System.out.println("remove:" + element);
            iterator2.remove();
        }
        if (!iterator2.hasNext()) System.out.println("没有东西了");
    }

    public static void ArrayList() {
        ArrayList al = new ArrayList();
        al.add(new Integer(1));
        al.add(new Integer(2));
        al.add(new Integer(4));
        al.add(new Integer(5));
        al.add(new Integer(6));
        al.add(new String("6"));//original
        for (int i = 0; i < al.size(); i++) {
            System.out.println("Element" + i + "=" + al.get(i));
        }
    }

    public static void LinkedList() {
        LinkedList li = new LinkedList();
        li.add("A");
        li.add("B");
        li.add("F");
        li.add("C");
        li.add("D");
        li.add("E");
        li.add("F");
        li.add("G");
        //insert into  link of last
        li.addLast("last");
        //insert into  link of head
        li.addFirst("head");
        System.out.println("all content:" + li);
        li.remove("F");
        System.out.println("all content:" + li);
        li.removeFirst();
        li.removeLast();
        //LinkedList
        Object val = li.get(2);//用val 容器装在li
        li.set(2, (String) val + "changed");
        System.out.println("li 被修改之后");
        System.out.println(li);
    }

    public static void Hashset() {
        Set set1 = new HashSet();
        if (set1.add("a")) {
            System.out.println("set1 add true");
        }
        if (set1.add("b")) {
            System.out.println("B add true");
        }
        set1.add("0000");
        set1.add("111");
        set1.add("222");
        System.out.println("collection set1 size:" + set1.size());
        System.out.println("collection set1 content:" + set1);
        set1.remove("0000");
        System.out.println("After  removal:" + set1);
        System.out.println("contain vlaue:" + set1.contains("0000"));
        System.out.println("After  removal:" + set1);
        Set set2 = new HashSet();
        set2.add("123");
        set2.add(set1);
        set1.clear();
        System.out.println("After clear" + set1);
        Iterator iterator = set2.iterator();
        System.out.println("output content by iterator" + set2);
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        Object s[] = set2.toArray();
        System.out.println("array");
        for (int i = 0; i < s.length; i++) {
            System.out.println(s[i]);
        }
    }

    public static void HashSet_2() {
        //这就是在展现 通过使用集合理顺数据
        class Name {
            String first;
            String last;

            public Name(String first, String last) {
                this.first = first;
                this.last = last;
            }

            public int hashCode() {
                //返回哈希表 我也不知道是什么东西 但以后肯定会碰到的
                final int prime = 31;
                int result = 1;
                result = prime * result + ((this.first == null) ? 0 : first.hashCode());
                result = prime * result + ((this.last == null) ? 0 : first.hashCode());
                return result;
            }

            public boolean equals(Object obj) {
                if (this == obj) {
                    return true;
                }
                if (this == null)//判断本身？
                {
                    return false;
                }
                if (getClass() != obj.getClass())// getClass Object的类 获取对象类型 judge object of genre
                {
                    return false;
                }
                Name other = (Name) obj;
                if (first == null) {
                    if (other.first != null) {
                        return false;
                    } else if (!first.equals(other.first)) {
                        return false;
                    }
                }
                if (last == null) {
                    if (other.last != null) {
                        return false;
                    } else if (!last.equals(other.last)) {
                        return false;
                    }
                }
                return true;
            }
            public String toString(){
                return "name[first=" + first + ",last=" + last + "]";
            }
        }
        HashSet<Name> hs = new HashSet<Name>();
        hs.add(new Name("wang", "wu"));
        hs.add(new Name("wang", "wu"));
        hs.add(new Name("Sang", "wu"));
        hs.add(new Name("Sang", "wu"));
        hs.add(new Name("Sang", "wu2"));
        Iterator tmp=hs.iterator();
        while(tmp.hasNext())
        {
            System.out.println(tmp.next());
        }
    }
    public static void TreeSet(){
        TreeSet ts=new TreeSet();
        ts.add("A");
        ts.add("B");
        ts.add("C");
        ts.add("D");
        ts.add("E");
        System.out.println(ts);
    }
    public static void TreeSet_Comparable(){
        class R implements Comparable{
            int count;
            public R(int count){
                this.count=count;
            }
            public String toString(){
                return "R(count 属性:"+count+")";
//                return " "+count;
            }
            public boolean equals(Object obj)
            {
                if(this==obj)
                    return true;
                if(obj==null)
                    return false;
                if(getClass()!=obj.getClass())
                    return false;
                R other=(R)obj;
                if(count!=other.count)
                    return false;
                return true;
            }
            public int compareTo(Object obj){
                R r=(R) obj;
                if(this.count<r.count)
                {
                    return 1;
                }
                else if(this.count==r.count){
                    return 0;
                }
                else{
                    return -1;
                }
            }
        }
        TreeSet  ts=new TreeSet();
        ts.add(new R(5));
        ts.add(new R(1));
        ts.add(new R(-1));
        ts.add(new R(0));
        System.out.println(ts);
    }
    public static void Map_demo(){
        HashMap hm=new HashMap();
        hm.put("john Doe", new Double(123.123));
        hm.put("john Doe", new Double(1213.1123));
        hm.put("john Doe2", new Double(123.1233));
        Set set=hm.entrySet();
        Iterator i=set.iterator();
        while(i.hasNext()){
            System.out.println(i.next());
        }

    }
    public static void Map_demo_2(){
        class Code{
            final int id;
            Code(int i) {
                this.id=i;
            }
            public boolean equals(Object anObject){
                if(anObject instanceof Code) {
                    Code other=(Code) anObject;
                    return this.id==other.id;
                }
                return false;
            }
            public String  toString(){
                return "身份证:"+id;
            }
            public int hashCode(){
                return id;
            }
        }
        class Person{
            String name;
            Code id;
            public Person(String name,Code id){
                this.name=name;
                this.id=id;
            }
            public String getName(){
                 return this.name;
            }
            public void setName(String name)
            {
                    this.name=name;
            }
            public Code getId(){
                return this.id;
            }
            public void setId(Code id){
                this.id=id;
            }
            public String toString(){
                return "姓名是"+this.getName();
            }
        }

        HashMap map =new HashMap();
        Person p1=new Person("黄江宇",new Code(123424));
        map.put(p1.id,p1);
        Person p2=new Person("红江宇",new Code(1234224));
        map.put(p2.id,p2);
        Person p3=new Person("绿江宇",new Code(1234324));
        map.put(p3.id,p3);
        System.out.println("HashMap 中的人员信息"+map);
        Person p4 =new Person("白江宇",new Code(1234324));
        map.put(p4.id,p4);


    }
    public static  void TreeMap(){
        TreeMap tm =new TreeMap();
        tm.put(new Integer(10000 - 2000), "张三");
        tm.put(new Integer(10000 - 1500), "李四");
        tm.put(new Integer(10000 - 2500), "王五");
        tm.put(new Integer(10000 - 5000), "赵六");
        Collection col=tm.values();
        Iterator i =col.iterator();
        System.out.println("按工资高低排序");
        while (i.hasNext()) {
            System.out.println(i.next());
        }

    }
    public static  void  test(){

        class tea{
            public String name;
            public String value;
            tea(String name,String value){
                this.name=name;
                this.value=value;
            }
            public String toString(){

                return value;
            }
        }
        TreeMap teacher_date=new TreeMap();
        tea tea1 = new tea("Tom", "jsp");
        tea tea2 = new tea("To1", "jsp1");
        tea tea3 = new tea("To2", "jsp12");
        teacher_date.put(tea1.name,tea2);
        teacher_date.put(tea2.name,tea2);
        teacher_date.put(tea3.name,tea2);

        System.out.println(teacher_date);
        System.out.println(teacher_date);
        Set set =teacher_date.keySet();
//        Collection a=teacher_date.values();
//        Iterator b=a.iterator();
        Object a[]=set.toArray();

        System.out.println(set);



    }
}
